package com.aw.guiguzi.module.setting

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import com.aw.baselib.base.AwMvpBindingActivity
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwFormatCheckUtils
import com.aw.guiguzi.R
import com.aw.guiguzi.databinding.ActivitySettingModifyPwdBinding
import com.aw.guiguzi.event.EventLocalReLoginType
import com.aw.guiguzi.module.setting.presenters.ModifyPwdPresent
import com.aw.guiguzi.module.setting.views.ModifyPwdView
import com.aw.guiguzi.util.ReLoginUtil.reLogin

class ModifyPwdActivity :
    AwMvpBindingActivity<ModifyPwdPresent?, ActivitySettingModifyPwdBinding?>(),
    ModifyPwdView.View {
    private var isOpenEyeOld = false
    private var isOpenEyeNew = false
    private var isOpenEyeNewRe = false
    override fun createPresenter(): ModifyPwdPresent {
        return ModifyPwdPresent(this)
    }

    override fun getViewBinding(): ActivitySettingModifyPwdBinding {
        return ActivitySettingModifyPwdBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        setToolbar("修改密码", null)
    }

    override fun initData() {
        super.initData()
        binding?.etPwdOld?.transformationMethod = PasswordTransformationMethod.getInstance()
        binding?.etPwdNew?.transformationMethod = PasswordTransformationMethod.getInstance()
        binding?.etPwdNewRe?.transformationMethod = PasswordTransformationMethod.getInstance()
    }

    override fun initListener() {
        super.initListener()
        binding?.ivEyeOld?.setOnClickListener { v: View? ->
            if (isOpenEyeOld) {
                binding?.ivEyeOld?.setImageResource(R.drawable.icon_eye_off)
                isOpenEyeOld = false
                binding?.etPwdOld?.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                binding?.ivEyeOld?.setImageResource(R.drawable.icon_eye_on)
                isOpenEyeOld = true
                binding?.etPwdOld?.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
            binding?.etPwdOld?.setSelection(getText(binding?.etPwdOld).length)
        }
        binding?.ivEyeNew?.setOnClickListener { v: View? ->
            if (isOpenEyeNew) {
                binding?.ivEyeNew?.setImageResource(R.drawable.icon_eye_off)
                isOpenEyeNew = false
                binding?.etPwdNew?.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                binding?.ivEyeNew?.setImageResource(R.drawable.icon_eye_on)
                isOpenEyeNew = true
                binding?.etPwdNew?.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
            binding?.etPwdNew?.setSelection(getText(binding?.etPwdNew).length)
        }
        binding?.ivEyeNewRe?.setOnClickListener { v: View? ->
            if (isOpenEyeNewRe) {
                binding?.ivEyeNewRe?.setImageResource(R.drawable.icon_eye_off)
                isOpenEyeNewRe = false
                binding?.etPwdNewRe?.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                binding?.ivEyeNewRe?.setImageResource(R.drawable.icon_eye_on)
                isOpenEyeNewRe = true
                binding?.etPwdNewRe?.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
            binding?.etPwdNewRe?.setSelection(getText(binding?.etPwdNewRe).length)
        }
        findViewById<View>(R.id.tv_confirm).setOnClickListener { v: View? ->
            if (AwDataUtil.isEmpty(getText(binding?.etPwdOld))) {
                showMsg("请输入原密码")
                return@setOnClickListener
            }
            if (AwDataUtil.isEmpty(getText(binding?.etPwdNew))) {
                showMsg("请输入新密码")
                return@setOnClickListener
            }
            if (AwDataUtil.isEmpty(getText(binding?.etPwdNewRe))) {
                showMsg("请再次输入新密码")
                return@setOnClickListener
            }
            if (getText(binding?.etPwdNew).length < 6) {
                showMsg("密码长度不可小于6位")
                return@setOnClickListener
            }
            if (!AwFormatCheckUtils.isLetterDigitAll(getText(binding?.etPwdNew))) {
                showMsg("密码需包含字母与数字")
                return@setOnClickListener
            }
            if (getText(binding?.etPwdNew) != getText(binding?.etPwdNewRe)) {
                showMsg("两次密码输入不一致")
                return@setOnClickListener
            }
            if (getText(binding?.etPwdOld) == getText(binding?.etPwdNew)) {
                showMsg("新密码不可与原密码一致")
                return@setOnClickListener
            }
            mPresenter!!.verifyPwd(getText(binding?.etPwdOld), getText(binding?.etPwdNew))
        }
    }

    override fun updatePwdSuccess() {
        showMsg("密码修改成功，请重新登陆")
        postEvent(EventLocalReLoginType(null))
        reLogin(mActivity)
    }
}